﻿using AutoMapper;
using ERPSystem.Common.Domains.PurchaseWarehousing;
using ERPSystem.Common.Domains.supplier;
using ERPSystem.Common.DTO;
using ERPSystem.Common.Infrastructure.IRepository;
using ERPSystem.Enum.CommonErrorCodes;
using MediatR;
using NPOI.SS.Formula.Functions;

namespace BasicInformationWrite.API.BasicInformationCommons.suppliers
{
    public class PurchaseIncomingMaterialsCreateHander : IRequestHandler<PurchaseIncomingMaterialsCreateCommand, Result>
    {
        private readonly IMapper _mapper;
        private readonly IBaseRepository<PurchaseWarehousing> _purchaseWarehousingRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="purchaseWarehousingRepository"></param>
        public PurchaseIncomingMaterialsCreateHander(IMapper mapper, IBaseRepository<PurchaseWarehousing> purchaseWarehousingRepository)
        {
            _mapper = mapper;
            _purchaseWarehousingRepository = purchaseWarehousingRepository;
        }
        /// <summary>
        /// 处理采购入库命令
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(PurchaseIncomingMaterialsCreateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };

            try
            {
                var supplier = _mapper.Map<PurchaseWarehousing>(request);
                var list = await _purchaseWarehousingRepository.Add(supplier);
                if (list > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "添加成功";
                }
                else
                {
                    result.Msg = "添加失败";
                }

            }
            catch (Exception)
            {

                throw;
            }
            return result;

        }
    }
}
